home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / glass / glass.lha / GLASS / widgets / Viewport.c < prev    next >
C/C++ Source or Header  |  1991-02-01  |  27KB  |  872 lines

  1. #ifndef lint
  2. static char Xrcsid[] = "$XConsortium: Viewport.c,v 1.48 89/12/15 11:37:22 kit Exp $";
  3. #endif /* lint */
  4.  
  5.  
  6. /***********************************************************
  7. Copyright 1987, 1988 by Digital Equipment Corporation, Maynard, Massachusetts,
  8. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  9.  
  10.                         All Rights Reserved
  11.  
  12. Permission to use, copy, modify, and distribute this software and its 
  13. documentation for any purpose and without fee is hereby granted, 
  14. provided that the above copyright notice appear in all copies and that
  15. both that copyright notice and this permission notice appear in 
  16. supporting documentation, and that the names of Digital or MIT not be
  17. used in advertising or publicity pertaining to distribution of the
  18. software without specific, written prior permission.  
  19.  
  20. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  21. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  22. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  23. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  25. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  26. SOFTWARE.
  27.  
  28. ******************************************************************/
  29.  
  30. #include <X11/IntrinsicP.h>
  31. #include <X11/StringDefs.h>
  32. #include <X11/Xaw/XawInit.h>
  33. #include <X11/Xmu/Misc.h>
  34. #include <X11/Xaw/Scrollbar.h>
  35. #include "ViewportP.h"
  36.  
  37. static void ScrollUpDownProc(), ThumbProc();
  38. static Boolean GetGeometry();
  39.  
  40. #define offset(field) XtOffset(ViewportWidget,viewport.field)
  41. static XtResource resources[] = {
  42.     {XtNforceBars, XtCBoolean, XtRBoolean, sizeof(Boolean),
  43.      offset(forcebars), XtRImmediate, (caddr_t)False},
  44.     {XtNallowHoriz, XtCBoolean, XtRBoolean, sizeof(Boolean),
  45.      offset(allowhoriz), XtRImmediate, (caddr_t)False},
  46.     {XtNallowVert, XtCBoolean, XtRBoolean, sizeof(Boolean),
  47.      offset(allowvert), XtRImmediate, (caddr_t)False},
  48.     {XtNuseBottom, XtCBoolean, XtRBoolean, sizeof(Boolean),
  49.      offset(usebottom), XtRImmediate, (caddr_t)False},
  50.     {XtNuseRight, XtCBoolean, XtRBoolean, sizeof(Boolean),
  51.      offset(useright), XtRImmediate, (caddr_t)False},
  52. };
  53. #undef offset
  54.  
  55. static void Initialize(), ConstraintInitialize(),
  56.     Realize(), Resize(), ChangeManaged();
  57. static Boolean SetValues(), Layout();
  58. static XtGeometryResult GeometryManager(), PreferredGeometry();
  59.  
  60. #define superclass    (&formClassRec)
  61. ViewportClassRec viewportClassRec = {
  62.   { /* core_class fields */
  63.     /* superclass      */    (WidgetClass) superclass,
  64.     /* class_name      */    "Viewport",
  65.     /* widget_size      */    sizeof(ViewportRec),
  66.     /* class_initialize      */    XawInitializeWidgetSet,
  67.     /* class_part_init    */    NULL,
  68.     /* class_inited      */    FALSE,
  69.     /* initialize      */    Initialize,
  70.     /* initialize_hook    */    NULL,
  71.     /* realize          */    Realize,
  72.     /* actions          */    NULL,
  73.     /* num_actions      */    0,
  74.     /* resources      */    resources,
  75.     /* num_resources      */    XtNumber(resources),
  76.     /* xrm_class      */    NULLQUARK,
  77.     /* compress_motion      */    TRUE,
  78.     /* compress_exposure  */    TRUE,
  79.     /* compress_enterleave*/    TRUE,
  80.     /* visible_interest      */    FALSE,
  81.     /* destroy          */    NULL,
  82.     /* resize          */    Resize,
  83.     /* expose          */    XtInheritExpose,
  84.     /* set_values      */    SetValues,
  85.     /* set_values_hook    */    NULL,
  86.     /* set_values_almost  */    XtInheritSetValuesAlmost,
  87.     /* get_values_hook    */    NULL,
  88.     /* accept_focus      */    NULL,
  89.     /* version            */    XtVersion,
  90.     /* callback_private      */    NULL,
  91.     /* tm_table          */    NULL,
  92.     /* query_geometry     */    PreferredGeometry,
  93.     /* display_accelerator*/    XtInheritDisplayAccelerator,
  94.     /* extension          */    NULL
  95.   },
  96.   { /* composite_class fields */
  97.     /* geometry_manager      */    GeometryManager,
  98.     /* change_managed      */    ChangeManaged,
  99.     /* insert_child      */    XtInheritInsertChild,
  100.     /* delete_child      */    XtInheritDeleteChild,
  101.     /* extension          */    NULL
  102.   },
  103.   { /* constraint_class fields */
  104.     /* subresourses      */    NULL,
  105.     /* subresource_count  */    0,
  106.     /* constraint_size      */    sizeof(ViewportConstraintsRec),
  107.     /* initialize      */    ConstraintInitialize,
  108.     /* destroy          */    NULL,
  109.     /* set_values      */    NULL,
  110.     /* extension          */    NULL
  111.   },
  112.   { /* form_class fields */
  113.     /* layout          */    Layout
  114.   },
  115.   { /* viewport_class fields */
  116.     /* empty          */    0
  117.   }
  118. };
  119.  
  120.  
  121. WidgetClass viewportWidgetClass = (WidgetClass)&viewportClassRec;
  122.  
  123. static Widget CreateScrollbar(w, horizontal)
  124.     ViewportWidget w;
  125.     Boolean horizontal;
  126. {
  127.     Widget clip = w->viewport.clip;
  128.     ViewportConstraints constraints =
  129.     (ViewportConstraints)clip->core.constraints;
  130.     static Arg barArgs[] = {
  131.     {XtNorientation, NULL},
  132.     {XtNlength, NULL},
  133.     {XtNleft, NULL},
  134.     {XtNright, NULL},
  135.     {XtNtop, NULL},
  136.     {XtNbottom, NULL},
  137.     {XtNmappedWhenManaged, False},
  138.     };
  139.     Widget bar;
  140.  
  141.     XtSetArg(barArgs[0], XtNorientation,
  142.           horizontal ? XtorientHorizontal : XtorientVertical );
  143.     XtSetArg(barArgs[1], XtNlength,
  144.          horizontal ? clip->core.width : clip->core.height);
  145.     XtSetArg(barArgs[2], XtNleft,
  146.          (!horizontal && w->viewport.useright) ? XtChainRight : XtChainLeft);
  147.     XtSetArg(barArgs[3], XtNright,
  148.          (!horizontal && !w->viewport.useright) ? XtChainLeft : XtChainRight);
  149.     XtSetArg(barArgs[4], XtNtop,
  150.          (horizontal && w->viewport.usebottom) ? XtChainBottom: XtChainTop);
  151.     XtSetArg(barArgs[5], XtNbottom,
  152.          (horizontal && !w->viewport.usebottom) ? XtChainTop: XtChainBottom);
  153.  
  154.     bar = XtCreateWidget( (horizontal ? "horizontal" : "vertical"),
  155.               scrollbarWidgetClass, (Widget)w,
  156.               barArgs, XtNumber(barArgs) );
  157.     XtAddCallback( bar, XtNscrollProc, ScrollUpDownProc, (caddr_t)w );
  158.     XtAddCallback( bar, XtNjumpProc, ThumbProc, (caddr_t)w );
  159.  
  160.     if (horizontal) {
  161.     w->viewport.horiz_bar = bar;
  162.     constraints->form.vert_base = bar;
  163.     }
  164.     else {
  165.     w->viewport.vert_bar = bar;
  166.     constraints->form.horiz_base = bar;
  167.     }
  168.  
  169.     XtManageChild( bar );
  170.  
  171.     return bar;
  172. }
  173.  
  174. /* ARGSUSED */
  175. static void Initialize(request, new)
  176.     Widget request, new;
  177. {
  178.     ViewportWidget w = (ViewportWidget)new;
  179.     static Arg clip_args[5];
  180.     Cardinal num_args;
  181.     Widget h_bar, v_bar;
  182.     Dimension clip_height, clip_width;
  183.  
  184.     w->form.default_spacing = 0;  /* Reset the default spacing to 0 pixels. */
  185.  
  186.  
  187. /* 
  188.  * Initialize all widget pointers to NULL.
  189.  */
  190.  
  191.     w->viewport.child = (Widget) NULL;
  192.     w->viewport.horiz_bar = w->viewport.vert_bar = (Widget)NULL;
  193.  
  194. /* 
  195.  * Create Clip Widget.
  196.  */
  197.  
  198.     num_args = 0;
  199.     XtSetArg(clip_args[num_args], XtNborderWidth, 0); num_args++;
  200.     XtSetArg(clip_args[num_args], XtNleft, XtChainLeft); num_args++;
  201.     XtSetArg(clip_args[num_args], XtNright, XtChainRight); num_args++;
  202.     XtSetArg(clip_args[num_args], XtNtop, XtChainTop); num_args++;
  203.     XtSetArg(clip_args[num_args], XtNbottom, XtChainBottom); num_args++;
  204.  
  205.     w->viewport.clip = XtCreateManagedWidget("clip", widgetClass, new,
  206.                          clip_args, num_args);
  207.  
  208.  
  209.     if (!w->viewport.forcebars) 
  210.         return;         /* If we are not forcing the bars then we are done. */
  211.  
  212.     if (w->viewport.allowhoriz) 
  213.         (void) CreateScrollbar(w, True);
  214.     if (w->viewport.allowvert) 
  215.         (void) CreateScrollbar(w, False);
  216.  
  217.     h_bar = w->viewport.horiz_bar;
  218.     v_bar = w->viewport.vert_bar;
  219.  
  220. /*
  221.  * Set the clip widget to the correct height.
  222.  */
  223.  
  224.     clip_width = w->core.width;
  225.     clip_height = w->core.height;
  226.  
  227.     if ( (h_bar != NULL) &&
  228.      (w->core.width > h_bar->core.width + h_bar->core.border_width) )
  229.         clip_width -= h_bar->core.width + h_bar->core.border_width;
  230.     
  231.     if ( (v_bar != NULL) &&
  232.      (w->core.height > v_bar->core.height + v_bar->core.border_width) )
  233.         clip_height -= v_bar->core.height + v_bar->core.border_width;
  234.  
  235.     num_args = 0;
  236.     XtSetArg(clip_args[num_args], XtNwidth, clip_width); num_args++;
  237.     XtSetArg(clip_args[num_args], XtNheight, clip_height); num_args++;
  238.     XtSetValues(w->viewport.clip, clip_args, num_args);
  239. }
  240.  
  241. /* ARGSUSED */
  242. static void ConstraintInitialize(request, new)
  243.     Widget request, new;
  244. {
  245.     ((ViewportConstraints)new->core.constraints)->viewport.reparented = False;
  246. }
  247.  
  248. static void Realize(widget, value_mask, attributes)
  249.     Widget widget;
  250.     XtValueMask *value_mask;
  251.     XSetWindowAttributes *attributes;
  252. {
  253.     ViewportWidget w = (ViewportWidget)widget;
  254.     register Widget child = w->viewport.child;
  255.     register Widget clip = w->viewport.clip;
  256.  
  257.     *value_mask |= CWBitGravity;
  258.     attributes->bit_gravity = NorthWestGravity;
  259.     (*superclass->core_class.realize)(widget, value_mask, attributes);
  260.  
  261.     (*w->core.widget_class->core_class.resize)(widget);    /* turn on bars */
  262.  
  263.     if (child != (Widget)NULL) {
  264.     XtMoveWidget( child, (Position)0, (Position)0 );
  265.     XtRealizeWidget( clip );
  266.     XtRealizeWidget( child );
  267.     XReparentWindow( XtDisplay(w), XtWindow(child), XtWindow(clip),
  268.              (Position)0, (Position)0 );
  269.     XtMapWidget( child );
  270.     }
  271. }
  272.  
  273. /* ARGSUSED */
  274. static Boolean SetValues(current, request, new)
  275.     Widget current, request, new;
  276. {
  277.     ViewportWidget w = (ViewportWidget)new;
  278.     ViewportWidget cw = (ViewportWidget)current;
  279.  
  280.     if (w->viewport.forcebars != cw->viewport.forcebars) {
  281.     if (w->viewport.forcebars) {
  282.         if (w->viewport.allowhoriz && w->viewport.horiz_bar == (Widget)NULL)
  283.         (void) CreateScrollbar( w, True );
  284.         if (w->viewport.allowvert && w->viewport.vert_bar == (Widget)NULL)
  285.         (void) CreateScrollbar( w, False );
  286.     }
  287.     }
  288.  
  289.     /* take care of bars, &tc. */
  290.     (*w->core.widget_class->core_class.resize)( new );
  291.     return False;
  292. }
  293.  
  294.  
  295. static void ChangeManaged(widget)
  296.     Widget widget;
  297. {
  298.     ViewportWidget w = (ViewportWidget)widget;
  299.     register int num_children = w->composite.num_children;
  300.     register Widget child, *childP;
  301.     register int i;
  302.  
  303.     child = (Widget)NULL;
  304.     for (childP=w->composite.children, i=0; i < num_children; childP++, i++) {
  305.     if (XtIsManaged(*childP)
  306.         && *childP != w->viewport.clip
  307.         && *childP != w->viewport.horiz_bar
  308.         && *childP != w->viewport.vert_bar)
  309.     {
  310.         child = *childP;
  311.         break;
  312.     }
  313.     }
  314.  
  315.     if (child != w->viewport.child) {
  316.     w->viewport.child = child;
  317.     if (child != (Widget)NULL) {
  318.         XtResizeWidget( child, child->core.width,
  319.                 child->core.height, (Dimension)0 );
  320.         if (XtIsRealized(widget)) {
  321.         ViewportConstraints constraints =
  322.             (ViewportConstraints)child->core.constraints;
  323.         if (!XtIsRealized(child)) {
  324.             Window window = XtWindow(w);
  325.             XtMoveWidget( child, (Position)0, (Position)0 );
  326. #ifdef notdef
  327.             /* this is dirty, but it saves the following code: */
  328.             XtRealizeWidget( child );
  329.             XReparentWindow( XtDisplay(w), XtWindow(child),
  330.                      XtWindow(w->viewport.clip),
  331.                      (Position)0, (Position)0 );
  332.             if (child->core.mapped_when_managed)
  333.             XtMapWidget( child );
  334. #else 
  335.             w->core.window = XtWindow(w->viewport.clip);
  336.             XtRealizeWidget( child );
  337.             w->core.window = window;
  338. #endif /* notdef */
  339.             constraints->viewport.reparented = True;
  340.         }
  341.         else if (!constraints->viewport.reparented) {
  342.             XReparentWindow( XtDisplay(w), XtWindow(child),
  343.                      XtWindow(w->viewport.clip),
  344.                      (Position)0, (Position)0 );
  345.             constraints->viewport.reparented = True;
  346.             if (child->core.mapped_when_managed)
  347.             XtMapWidget( child );
  348.         }
  349.         }
  350.         GetGeometry( widget, child->core.width, child->core.height );
  351.         (*((ViewportWidgetClass)w->core.widget_class)->form_class.layout)
  352.         ( (FormWidget)w, w->core.width, w->core.height );
  353.         /* %%% do we need to hide this child from Form?  */
  354.     }
  355.     }
  356.  
  357. #ifdef notdef
  358.     (*superclass->composite_class.change_managed)( widget );
  359. #endif
  360. }
  361.  
  362.  
  363. static void SetBar(w, top, length, total)
  364.     Widget w;
  365.     Position top;
  366.     Dimension length, total;
  367. {
  368.     XawScrollbarSetThumb(w, (float) top / total, (float) length / total );
  369. }
  370.  
  371. static void RedrawThumbs(w)
  372.   ViewportWidget w;
  373. {
  374.     register Widget child = w->viewport.child;
  375.     register Widget clip = w->viewport.clip;
  376.  
  377.     if (w->viewport.horiz_bar != (Widget)NULL)
  378.     SetBar( w->viewport.horiz_bar, -(child->core.x),
  379.             clip->core.width, child->core.width );
  380.  
  381.     if (w->viewport.vert_bar != (Widget)NULL)
  382.     SetBar( w->viewport.vert_bar, -(child->core.y),
  383.             clip->core.height, child->core.height );
  384. }
  385.  
  386.  
  387.  
  388. void MoveChild(w, x, y)
  389.     ViewportWidget w;
  390.     Position x, y;
  391. {
  392.     register Widget child = w->viewport.child;
  393.     register Widget clip = w->viewport.clip;
  394.  
  395.     /* make sure we never move past right/bottom borders */
  396.     if (-x + clip->core.width > child->core.width)
  397.     x = -(child->core.width - clip->core.width);
  398.  
  399.     if (-y + clip->core.height > child->core.height)
  400.     y = -(child->core.height - clip->core.height);
  401.  
  402.     /* make sure we never move past left/top borders */
  403.     if (x >= 0) x = 0;
  404.     if (y >= 0) y = 0;
  405.  
  406.     XtMoveWidget(child, x, y);
  407.  
  408.     RedrawThumbs(w);
  409. }
  410.  
  411.  
  412. static void ComputeLayout(widget, query, destroy_scrollbars)
  413.     Widget widget;        /* Viewport */
  414.     Boolean query;        /* query child's preferred geom? */
  415.     Boolean destroy_scrollbars;    /* destroy un-needed scrollbars? */
  416. {
  417.     ViewportWidget w = (ViewportWidget)widget;
  418.     register Widget child = w->viewport.child;
  419.     register Widget clip = w->viewport.clip;
  420.     ViewportConstraints constraints
  421.     = (ViewportConstraints)clip->core.constraints;
  422.     Boolean needshoriz, needsvert;
  423.     int clip_width, clip_height;
  424.     XtWidgetGeometry intended;
  425.     static void ComputeWithForceBars();
  426.  
  427.     if (child == (Widget) NULL) return;
  428.  
  429.     clip_width = w->core.width;
  430.     clip_height = w->core.height;
  431.     intended.request_mode = CWBorderWidth;
  432.     intended.border_width = 0;
  433.  
  434.     if (w->viewport.forcebars) {
  435.         needsvert = w->viewport.allowvert;
  436.         needshoriz = w->viewport.allowhoriz;
  437.         ComputeWithForceBars(widget, query, &intended, 
  438.                  &clip_width, &clip_height);
  439.     }
  440.     else {
  441.         Dimension prev_width, prev_height;
  442.     XtGeometryMask prev_mode;
  443.     XtWidgetGeometry preferred;
  444.  
  445.     needshoriz = needsvert = False;
  446.  
  447.     /*
  448.      * intended.{width,height} caches the eventual child dimensions,
  449.      * but we don't set the mode bits until after we decide that the
  450.      * child's preferences are not acceptable.
  451.      */
  452.  
  453.     if (!w->viewport.allowhoriz) 
  454.         intended.request_mode |= CWWidth;
  455.  
  456.     if (child->core.width < clip_width) 
  457.         intended.width = clip_width;
  458.     else
  459.         intended.width = child->core.width;
  460.  
  461.     if (child->core.height < clip_height) 
  462.         intended.height = clip_height;
  463.     else
  464.         intended.height = child->core.height;
  465.  
  466.     if (!w->viewport.allowvert) 
  467.         intended.request_mode |= CWHeight;
  468.  
  469.     if (!query) {
  470.         preferred.width = child->core.width;
  471.         preferred.height = child->core.height;
  472.     }
  473.     do { /* while intended != prev  */
  474.  
  475.         if (query) {
  476.             (void) XtQueryGeometry( child, &intended, &preferred );
  477.         if ( !(preferred.request_mode & CWWidth) )
  478.             preferred.width = intended.width;
  479.         if ( !(preferred.request_mode & CWHeight) )
  480.             preferred.height = intended.height;
  481.         }
  482.         prev_width = intended.width;
  483.         prev_height = intended.height;
  484.         prev_mode = intended.request_mode;
  485.         /*
  486.          * note that having once decided to turn on either bar
  487.          * we'll not change our mind until we're next resized,
  488.          * thus avoiding potential oscillations.
  489.          */
  490. #define CheckHoriz()                              \
  491.         if (w->viewport.allowhoriz && preferred.width > clip_width) { \
  492.             if (!needshoriz) {                      \
  493.             Widget bar;                              \
  494.             needshoriz = True;                          \
  495.             if ((bar = w->viewport.horiz_bar) == (Widget)NULL)    \
  496.                 bar = CreateScrollbar(w, True);                  \
  497.             clip_height -= bar->core.height +                  \
  498.                        bar->core.border_width;          \
  499.             if (clip_height < 1) clip_height = 1;          \
  500.         }                              \
  501.             intended.width = preferred.width;              \
  502.         }
  503. /*enddef*/
  504.         CheckHoriz();
  505.         if (w->viewport.allowvert && preferred.height > clip_height) {
  506.             if (!needsvert) {
  507.             Widget bar;
  508.             needsvert = True;
  509.             if ((bar = w->viewport.vert_bar) == (Widget)NULL)
  510.                 bar = CreateScrollbar(w, False);
  511.             clip_width -= bar->core.width + bar->core.border_width;
  512.             if (clip_width < 1) clip_width = 1;
  513.             CheckHoriz();
  514.         }
  515.         intended.height = preferred.height;
  516.         }
  517.         if (!w->viewport.allowhoriz || preferred.width < clip_width) {
  518.             intended.width = clip_width;
  519.         intended.request_mode |= CWWidth;
  520.         }
  521.         if (!w->viewport.allowvert || preferred.height < clip_height) {
  522.             intended.height = clip_height;
  523.         intended.request_mode |= CWHeight;
  524.         }
  525.     } while (intended.request_mode != prev_mode
  526.          || (intended.request_mode & CWWidth
  527.              && intended.width != prev_width)
  528.          || (intended.request_mode & CWHeight
  529.              && intended.height != prev_height));
  530.     }
  531.  
  532.     if (XtIsRealized(clip))
  533.     XRaiseWindow( XtDisplay(clip), XtWindow(clip) );
  534.  
  535.     XtMoveWidget( clip,
  536.           needsvert ? (w->viewport.useright ? 0 :
  537.                    w->viewport.vert_bar->core.width +
  538.                    w->viewport.vert_bar->core.border_width) : 0,
  539.           needshoriz ? (w->viewport.usebottom ? 0 :
  540.                 w->viewport.horiz_bar->core.height +
  541.                     w->viewport.horiz_bar->core.border_width) : 0);
  542.     XtResizeWidget( clip, (Dimension)clip_width,
  543.             (Dimension)clip_height, (Dimension)0 );
  544.     
  545.     if (w->viewport.horiz_bar != (Widget)NULL) {
  546.     register Widget bar = w->viewport.horiz_bar;
  547.     if (!needshoriz) {
  548.         constraints->form.vert_base = (Widget)NULL;
  549.         if (destroy_scrollbars) {
  550.         XtDestroyWidget( bar );
  551.         w->viewport.horiz_bar = (Widget)NULL;
  552.         }
  553.     }
  554.     else {
  555.         register int bw = bar->core.border_width;
  556.         XtResizeWidget( bar, clip_width, bar->core.height, bw );
  557.         XtMoveWidget( bar,
  558.               ((needsvert && !w->viewport.useright)
  559.                ? w->viewport.vert_bar->core.width
  560.                : -bw),
  561.               (w->viewport.usebottom
  562.                 ? w->core.height - bar->core.height - bw
  563.                 : -bw) );
  564.         XtSetMappedWhenManaged( bar, True );
  565.     }
  566.     }
  567.  
  568.     if (w->viewport.vert_bar != (Widget)NULL) {
  569.     register Widget bar = w->viewport.vert_bar;
  570.     if (!needsvert) {
  571.         constraints->form.horiz_base = (Widget)NULL;
  572.         if (destroy_scrollbars) {
  573.         XtDestroyWidget( bar );
  574.         w->viewport.vert_bar = (Widget)NULL;
  575.         }
  576.     }
  577.     else {
  578.         register int bw = bar->core.border_width;
  579.         XtResizeWidget( bar, bar->core.width, clip_height, bw );
  580.         XtMoveWidget( bar,
  581.               (w->viewport.useright
  582.                ? w->core.width - bar->core.width - bw 
  583.                : -bw),
  584.               ((needshoriz && !w->viewport.usebottom)
  585.                 ? w->viewport.horiz_bar->core.height
  586.                 : -bw) );
  587.         XtSetMappedWhenManaged( bar, True );
  588.     }
  589.     }
  590.  
  591.     if (child != (Widget)NULL) {
  592.     XtResizeWidget( child, (Dimension)intended.width,
  593.                 (Dimension)intended.height, (Dimension)0 );
  594.     MoveChild(w,
  595.           needshoriz ? child->core.x : 0,
  596.           needsvert ? child->core.y : 0);
  597.     }
  598. }
  599.  
  600. /*      Function Name: ComputeWithForceBars
  601.  *      Description: Computes the layout give forcebars is set.
  602.  *      Arguments: widget - the viewport widget.
  603.  *                 query - whether or not to query the child.
  604.  *                 intended - the cache of the childs height is
  605.  *                            stored here ( USED AND RETURNED ).
  606.  *                 clip_width, clip_height - size of clip window.
  607.  *                                           (USED AND RETURNED ).
  608.  *      Returns: none.
  609.  */
  610.  
  611. static void
  612. ComputeWithForceBars(widget, query, intended, clip_width, clip_height)
  613. Widget widget;
  614. Boolean query;
  615. XtWidgetGeometry * intended;
  616. int *clip_width, *clip_height;
  617. {
  618.     ViewportWidget w = (ViewportWidget)widget;
  619.     register Widget child = w->viewport.child;
  620.     XtWidgetGeometry preferred;
  621.  
  622. /*
  623.  * If forcebars then needs = allows = has.
  624.  * Thus if needsvert is set it MUST have a scrollbar.
  625.  */
  626.  
  627.     if (w->viewport.allowvert)
  628.         *clip_width -= w->viewport.vert_bar->core.width +
  629.                       w->viewport.vert_bar->core.border_width;
  630.  
  631.     if (w->viewport.allowhoriz)
  632.         *clip_height -= w->viewport.horiz_bar->core.height +
  633.                w->viewport.horiz_bar->core.border_width;
  634.  
  635.     AssignMax( *clip_width, 1 );
  636.     AssignMax( *clip_height, 1 );
  637.  
  638.     if (!w->viewport.allowvert) {
  639.         intended->height = *clip_height;
  640.         intended->request_mode = CWHeight;
  641.     }
  642.     if (!w->viewport.allowhoriz) {
  643.         intended->width = *clip_width;
  644.         intended->request_mode = CWWidth;
  645.     }
  646.  
  647.     if ( query ) {
  648.         if ( (w->viewport.allowvert || w->viewport.allowhoriz) ) { 
  649.         XtQueryGeometry( child, intended, &preferred );
  650.       
  651.         if ( !(intended->request_mode & CWWidth) )
  652.             if ( preferred.request_mode & CWWidth )
  653.             intended->width = preferred.width;
  654.         else
  655.             intended->width = child->core.width;
  656.  
  657.         if ( !(intended->request_mode & CWHeight) )
  658.             if ( preferred.request_mode & CWHeight )
  659.             intended->height = preferred.height;
  660.         else
  661.             intended->height = child->core.height;
  662.     }
  663.     }
  664.     else {
  665.         if (w->viewport.allowvert)
  666.         intended->height = child->core.height;
  667.     if (w->viewport.allowhoriz)
  668.         intended->width = child->core.width;
  669.     }
  670.  
  671.     AssignMax(intended->width, *clip_width);
  672.     AssignMax(intended->height, *clip_height);
  673. }
  674.  
  675. static void Resize(widget)
  676.     Widget widget;
  677. {
  678.     ComputeLayout( widget, /*query=*/True, /*destroy=*/True );
  679. }
  680.  
  681.  
  682. /* ARGSUSED */
  683. static Boolean Layout(w, width, height)
  684.     FormWidget w;
  685.     Dimension width, height;
  686. {
  687.     ComputeLayout( (Widget)w, /*query=*/True, /*destroy=*/True );
  688.     w->form.preferred_width = w->core.width;
  689.     w->form.preferred_height = w->core.height;
  690.     return False;
  691. }
  692.  
  693.  
  694. static void ScrollUpDownProc(widget, closure, call_data)
  695.     Widget widget;
  696.     caddr_t closure;
  697.     caddr_t call_data;
  698. {
  699.     ViewportWidget w = (ViewportWidget)closure;
  700.     register Widget child = w->viewport.child;
  701.     int pix = (int)call_data;
  702.     Position x, y;
  703.  
  704.     if (child == NULL) return;    /* no child to scroll. */
  705.  
  706.     x = child->core.x - ((widget == w->viewport.horiz_bar) ? pix : 0);
  707.     y = child->core.y - ((widget == w->viewport.vert_bar) ? pix : 0);
  708.     MoveChild(w, x, y);
  709. }
  710.  
  711.  
  712. /* ARGSUSED */
  713. static void ThumbProc(widget, closure, percent)
  714.     Widget widget;
  715.     caddr_t closure;
  716.     float *percent;
  717. {
  718.     ViewportWidget w = (ViewportWidget)closure;
  719.     register Widget child = w->viewport.child;
  720.     Position x, y;
  721.  
  722.     if (child == NULL) return;    /* no child to scroll. */
  723.  
  724.     if (widget == w->viewport.horiz_bar)
  725. #ifdef macII                /* bug in the macII A/UX 1.0 cc */
  726.     x = (int)(-*percent * child->core.width);
  727. #else /* else not macII */
  728.     x = -(int)(*percent * child->core.width);
  729. #endif /* macII */
  730.     else
  731.     x = child->core.x;
  732.  
  733.     if (widget == w->viewport.vert_bar)
  734. #ifdef macII                /* bug in the macII A/UX 1.0 cc */
  735.     y = (int)(-*percent * child->core.height);
  736. #else /* else not macII */
  737.     y = -(int)(*percent * child->core.height);
  738. #endif /* macII */
  739.     else
  740.     y = child->core.y;
  741.  
  742.     MoveChild(w, x, y);
  743. }
  744.  
  745.  
  746. static XtGeometryResult GeometryManager(child, request, reply)
  747.     Widget child;
  748.     XtWidgetGeometry *request, *reply;
  749. {
  750.     ViewportWidget w = (ViewportWidget)child->core.parent;
  751.     Boolean rWidth = (Boolean)(request->request_mode & CWWidth);
  752.     Boolean rHeight = (Boolean)(request->request_mode & CWHeight);
  753.     XtWidgetGeometry allowed;
  754.     XtGeometryResult result;
  755.     Boolean reconfigured;
  756.     Dimension height_remaining;
  757.  
  758.     if (child != w->viewport.child
  759.     || request->request_mode & ~(CWWidth | CWHeight | CWBorderWidth)
  760.     || ((request->request_mode & CWBorderWidth)
  761.         && request->border_width > 0))
  762.     return XtGeometryNo;
  763.  
  764.     allowed = *request;
  765.  
  766.  
  767.     reconfigured = GetGeometry( (Widget)w,
  768.                     (rWidth ? request->width : w->core.width),
  769.                     (rHeight ? request->height : w->core.height)
  770.                   );
  771.  
  772.     height_remaining = w->core.height;
  773.     if (rWidth && w->core.width != request->width) {
  774.     if (w->viewport.allowhoriz && request->width > w->core.width) {
  775.         /* horizontal scrollbar will be needed so possibly reduce height */
  776.         Widget bar; 
  777.         if ((bar = w->viewport.horiz_bar) == (Widget)NULL)
  778.         bar = CreateScrollbar( w, True );
  779.         height_remaining -= bar->core.height + bar->core.border_width;
  780.         reconfigured = True;
  781.     }
  782.     else {
  783.         allowed.width = w->core.width;
  784.     }
  785.     }
  786.     if (rHeight && height_remaining != request->height) {
  787.     if (w->viewport.allowvert && request->height > height_remaining) {
  788.         /* vertical scrollbar will be needed, so possibly reduce width */
  789.         if (!w->viewport.allowhoriz || request->width < w->core.width) {
  790.         Widget bar;
  791.         if ((bar = w->viewport.vert_bar) == (Widget)NULL)
  792.             bar = CreateScrollbar( w, False );
  793.         if (!rWidth) {
  794.             allowed.width = w->core.width;
  795.             allowed.request_mode |= CWWidth;
  796.         }
  797.         if (allowed.width > (bar->core.width + bar->core.border_width))
  798.             allowed.width -= bar->core.width + bar->core.border_width;
  799.         else
  800.             allowed.width = 1;
  801.         reconfigured = True;
  802.         }
  803.     }
  804.     else {
  805.         allowed.height = height_remaining;
  806.     }
  807.     }
  808.  
  809.     if (allowed.width != request->width || allowed.height != request->height) {
  810.     *reply = allowed;
  811.     result = XtGeometryAlmost;
  812.     }
  813.     else {
  814.     if (rWidth)  child->core.width = request->width;
  815.     if (rHeight) child->core.height = request->height;
  816.     result = XtGeometryYes;
  817.     }
  818.  
  819.     if (reconfigured)
  820.     ComputeLayout( (Widget)w,
  821.                /*query=*/ False,
  822.                /*destroy=*/ (result == XtGeometryYes) ? True : False );
  823.  
  824.     return result;
  825.   }
  826.  
  827.  
  828. static Boolean GetGeometry(w, width, height)
  829.     Widget w;
  830.     Dimension width, height;
  831. {
  832.     XtWidgetGeometry geometry;
  833.     XtGeometryResult result;
  834.  
  835.     if (width == w->core.width && height == w->core.height)
  836.     return False;
  837.  
  838.     geometry.request_mode = CWWidth | CWHeight;
  839.     geometry.width = width;
  840.     geometry.height = height;
  841.  
  842.     if (XtIsRealized(w)) {
  843.     if (((ViewportWidget)w)->viewport.allowhoriz && width > w->core.width)
  844.         geometry.width = w->core.width;
  845.     if (((ViewportWidget)w)->viewport.allowvert && height > w->core.height)
  846.         geometry.height = w->core.height;
  847.     } else {
  848.     /* This is the Realize call; we'll inherit a w&h iff none currently */
  849.     if (w->core.width != 0) {
  850.         if (w->core.height != 0) return False;
  851.         geometry.width = w->core.width;
  852.     }
  853.     if (w->core.height != 0) geometry.height = w->core.height;
  854.     }
  855.     if ((result = XtMakeGeometryRequest(w, &geometry, &geometry))
  856.     == XtGeometryAlmost)
  857.     result = XtMakeGeometryRequest(w, &geometry, NULL);
  858.  
  859.     return (result == XtGeometryYes);
  860. }
  861.  
  862. static XtGeometryResult PreferredGeometry(w, constraints, reply)
  863.     Widget w;
  864.     XtWidgetGeometry *constraints, *reply;
  865. {
  866.     if (((ViewportWidget)w)->viewport.child != NULL)
  867.     return XtQueryGeometry( ((ViewportWidget)w)->viewport.child,
  868.                    constraints, reply );
  869.     else
  870.     return XtGeometryYes;
  871. }
  872.